package com.course.web.component.custom;

import com.course.web.biz.WebClassBiz;
import com.course.web.biz.WebTeacherBiz;
import com.course.web.component.base.*;
import com.course.web.model.ClassItemVM;
import com.course.web.model.TeacherVM;
import com.course.web.view.StudentListView;
import com.vaadin.data.Binder;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Button;
import com.vaadin.ui.Layout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.renderers.DateRenderer;
import com.vaadin.ui.renderers.Renderer;

import java.text.SimpleDateFormat;

/**
 * Created by Justin on 2017/6/14.
 */
@SpringComponent
@ViewScope
public class ClassPlanGrid extends EditGrid<ClassItemVM> {

    private final WebClassBiz classBiz;
    private final WebTeacherBiz teacherBiz;

    private BaseList<TeacherVM> teacherList;

    public ClassPlanGrid(WebClassBiz classBiz, WebTeacherBiz teacherBiz) {
        super(ClassItemVM.class);
        this.classBiz = classBiz;
        this.teacherBiz = teacherBiz;
    }

    @Override
    protected void doInitMenu(Layout menu) {
        Button back = new Button("返回");
        menu.addComponent(back);
        back.addClickListener(event -> getUI().getNavigator().navigateTo(StudentListView.NAME));
    }

    @Override
    protected void doInitGrid(BaseGrid<ClassItemVM> grid) {
        grid.addColumn(ClassItemVM::getTask, "课程标题");
        grid.addColumn(ClassItemVM::getTeacher, "老师姓名");
        grid.addColumn(ClassItemVM::getRoom, "上课房间");
        Renderer renderer = new DateRenderer(new SimpleDateFormat("MM-dd HH:mm"));
        grid.addColumn(ClassItemVM::getDateTime, "上课时间").setRenderer(renderer);
        grid.addButton(this::updateShow, "安排教学");
    }

    @Override
    protected void doInitEdit(BaseEdit<ClassItemVM> edit) {
        VerticalLayout root = new VerticalLayout();
        TextField room = new TextField("上课房间");
        TimeBox time = new TimeBox("上课时间");
        this.teacherList = new BaseList<>();
        teacherList.setCaption("选择老师");
        teacherList.addColumn(TeacherVM::getId, "ID");
        teacherList.addColumn(TeacherVM::getName, "老师姓名");
        root.setMargin(false);
        root.addComponent(room);
        root.addComponent(time);
        root.addComponent(teacherList);
        edit.initEdit(root);

        Binder<ClassItemVM> binder = edit.binder();
        binder.bind(room, "room");
        binder.bind(time, "time");
        binder.bind(teacherList.adapter(), "teacherId");
        teacherList.setHandler(item -> binder.getBean().setTeacher(item.getName()));
    }

    @Override
    protected ClassItemVM doUpdate(ClassItemVM item) {
        if (item.getId() == 0) {
            return classBiz.createItem(item);
        } else {
            return classBiz.updateItem(item);
        }
    }

    @Override
    protected ClassItemVM doDelete(ClassItemVM item) {
        return classBiz.deleteItem(item);
    }

    public void enter(int studentId) {
        init(classBiz.findItemByStudent(studentId));
        teacherList.setItems(teacherBiz.list());
    }
}
